package controller

import (
	"haiqiu-go-admin/result"
	"haiqiu-go-admin/service"
	"net/http"
)

func (*PostApi) PostHandle(w http.ResponseWriter, r *http.Request) {
	switch r.Method {
	case http.MethodGet:
		PostAPI.postById(w, r)
		break
	case http.MethodPost:
		PostAPI.postAdd(w, r)
		break
	case http.MethodPut:
		PostAPI.postUpdate(w, r)
		break
	case http.MethodDelete:
		PostAPI.postDeleteBatch(w, r)
		break
	default:
		break
	}
}

// 根据ID查询数据
func (*PostApi) postById(w http.ResponseWriter, r *http.Request) {
	page, err := service.PostById(w, r)
	if err != nil {
		result.Fail(w, r, err)
		return
	}
	result.Ok(w, r, page)
}

// PostPage 分页查询
func (*PostApi) PostPage(w http.ResponseWriter, r *http.Request) {
	page, err := service.PostPage(w, r)
	if err != nil {
		result.Fail(w, r, err)
		return
	}
	result.Ok(w, r, page)
}

// 新增数据
func (*PostApi) postAdd(w http.ResponseWriter, r *http.Request) {
	add, err := service.PostAdd(w, r)
	if err != nil {
		result.Fail(w, r, err)
		return
	}
	result.Ok(w, r, add)
}

// 批量删除数据
func (*PostApi) postDeleteBatch(w http.ResponseWriter, r *http.Request) {
	add, err := service.PostDeleteBatch(w, r)
	if err != nil {
		result.Fail(w, r, err)
		return
	}
	result.Ok(w, r, add)
}

// 根据ID更新数据
func (*PostApi) postUpdate(w http.ResponseWriter, r *http.Request) {
	add, err := service.PostUpdate(w, r)
	if err != nil {
		result.Fail(w, r, err)
		return
	}
	result.Ok(w, r, add)
}

// PostList 查询所有数据
func (*PostApi) PostList(w http.ResponseWriter, r *http.Request) {
	postList, err := service.PostList(w, r)
	if err != nil {
		result.Fail(w, r, err)
		return
	}
	result.Ok(w, r, postList)
}
